#include "login.h"
#include "ui_login.h"
#include <QSqlDatabase>
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>
#include <QWidget>
#include "widget.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->label_4->hide();
    ui->setupUi(this);
}

Login::~Login()
{
    delete ui;
}

void Login::on_commandLinkButton_clicked()
{
//点击了登录按钮
    QString UserName = ui->lineEdit->text();
    QString PassWord = ui->lineEdit_2->text();
//获取到用户用户名和密码后,连接数据库进行查询,判断是否登录成功
    QSqlDatabase MyDataBase =  QSqlDatabase::addDatabase("QMYSQL");
    MyDataBase.setHostName("39.107.233.149");
    MyDataBase.setUserName("xxq");
    MyDataBase.setPassword("xiao3344520");
    MyDataBase.setDatabaseName("xxq");
    //打开数据库
    if(!MyDataBase.open()){
         //数据库打开失败
    QMessageBox::warning(this,"错误",MyDataBase.lastError().text());
    }else{
        QSqlQuery Query("SELECT * FROM `user` WHERE `username` ="+UserName+"and password ="+PassWord+";");
         while(Query.next()){
////            qDebug()<<Query.value(2).toString()<<endl;
            if(Query.value(0).toString().length())
            {
                //登录成功
                this->close();
                Widget *m = new Widget();
                m->show();
            }else{
                //登录失败
            }
         }
    }
}
